package com.xiaobingby.admin.sys.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.xiaobingby.admin.sys.entity.Permission;
import com.xiaobingby.admin.sys.entity.Role;
import com.xiaobingby.admin.sys.entity.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

/**
 * @author XiaoBingBy
 * @date 2019-01-08 19:48
 * @since 1.0
 */
public class UserDetailsDto extends User implements UserDetails {

    private List<Role> roles;

    private List<Permission> permissions;

    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        ArrayList<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        if (!CollectionUtils.isEmpty(roles)) {

        }
        if (!CollectionUtils.isEmpty(permissions)) {
            permissions.parallelStream().forEach(item -> {
                grantedAuthorities.add(new SimpleGrantedAuthority(item.getPermCode()));
            });
        }
        return grantedAuthorities;
    }

    @Override
    public String getPassword() {
        return super.getPassword();
    }

    @Override
    public String getUsername() {
        return super.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public List<Permission> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<Permission> permissions) {
        this.permissions = permissions;
    }

}
